package ajaxjs.sql;

import java.sql.Connection;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import ajaxjs.net.Request;

public class Util {
	private final static String insertSql = "INSERT INTO {tableName} ({columns}) VALUES ({values})";
	public static void fastCreate(HttpServletRequest request, Connection jdbcConn, String tableName, String parentTableName){
		String fields = getIntroSql(request, true);
		String values = getIntroSql(request, false);
		
		String createSql;
		createSql = insertSql.replace("{tableName}", tableName);
		createSql = createSql.replace("{columns}", fields + ",belongsTo");
		createSql = createSql.replace("{columns}", values + ",'{0}'".replace("{0}", parentTableName));
			
		DAO dao = new DAO(jdbcConn);
		dao.executeUpdate(createSql);	
	}
	public static void fastUpdate(){
		
	}
	
	/**
	 * 获取客户端请求的参数和值（转换为SQSL）
	 * @param request
	 * @return
	 */
	public static String getIntroSql(HttpServletRequest request, boolean isKey){
		Map<String, String> requestPair = Request.getClient_Data(request);

		String [] pairs = new String[requestPair.size()];
		
		int i = 0;
		for(String key : requestPair.keySet()){
			pairs[i++] = isKey ? key : "'" + requestPair.get(key) + "'";
		}
		
		return ajaxjs.Util.stringJoin(pairs, ",");
	}
}
